<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

include_once 'base_controller.php';

require_once APPPATH."libraries/PHPExcel.php";
require_once APPPATH."libraries/PHPExcel/IOFactory.php";

class Allocate extends Base_Controller 
{
    private $zerofalg = true;
    private $onefalg = true;
    private $twofalg = true;
    private $threefalg = true;
    private $fourfalg = true;
    private $fivefalg = true;
    private $sixfalg = true;
    private $sevenfalg = true;
    private $eightfalg = true;
    
    private $insertflag = 0;
    
    private $zerorow = 0;
    private $onerow = 0;
    private $tworow = 0;
    private $threerow = 0;
    private $fourrow = 0;
    private $fiverow = 0;
    private $sixrow = 0;
    private $sevenrow = 0;
    private $eightrow = 0;

    public function __construct() 
    {
        parent::__construct();
        $this->load->model('allocate_model');
        $this->load->library('phpexcel');
        $this->load->helper('file');
    }
    
    public function index()
    {
        //var_dump($_POST['']);exit;
        if( !empty ($_POST))
        {
            switch ($_POST['select'])
            {
                case 'payFlag':
                    $flagId = 20;
                    break;
                case 'balFlag':
                    $flagId = 18;
                    break;
                case 'proFlag':
                    $flagId = 19;
                    break;
                case 'alloFlag':
                    $flagId = 21;
                    break;
                case 'nullFlag':
                    $flagId = 0;
                    break;
                default :
                    return FALSE;
            }

            $list = $this->allocate_model->getInvoiceByFlagId($flagId);
            
            $data = array(
                'data'=>$list,
            );
            $this->load->view('allocate', $data);
        }
        else
        {
            $this->load->view('allocate');
        }
        
        //$this->load->view('allocate');
        $this->load->view('footer');
    }
    
    public function exportToExcel()
    {
        $string = read_file('./tmp/tradeGoodsList.php');
        $tradeGoodsList = unserialize($string);
        //var_dump($tradeGoodsList);exit;
        if( !empty ($tradeGoodsList))
        {
            error_reporting(E_ALL);
            // Create new PHPExcel object
            $objPHPExcel = new PHPExcel();
            
            for($i=1; $i<9; $i++)
            {
                $objWorksheet = $objPHPExcel->createSheet($i);
            }
            
            $data = $tradeGoodsList;
            foreach ($data as $k => $v)
            {
                //$v->ProviderNameGro
                if($v->stockOutflag != 2)
                {
                    $this->insertflag = 0;//插市场出货数量
                    //市场需要出货的
                    switch ($v->ProviderNameGro) 
                    {
                        case 'D3'://setActiveSheetIndex(0)
                            $objPHPExcel->setActiveSheetIndex(0);
                            if($this->zerofalg == TRUE)
                            {
                                // 设置当前表名
                                $objPHPExcel->getActiveSheet()->setTitle('大西豪3楼');
                                $this->setActiveSheet($objPHPExcel, 0, '大西豪3楼');
                            }
                            //调用插入数据函数
                            $this->insertData($this->zerorow, 0, 0, $v,$objPHPExcel);
                            $this->zerorow++;
                            break;
                        case 'D4'://setActiveSheetIndex(1)
                            $objPHPExcel->setActiveSheetIndex(1);
                            if($this->onefalg == TRUE)
                            {
                                $objPHPExcel->getActiveSheet()->setTitle('大西豪4楼');
                                $this->setActiveSheet($objPHPExcel, 1, '大西豪4楼');
                            }
                            //调用插入数据函数
                            $this->insertData($this->onerow, 1, 0, $v, $objPHPExcel);
                            $this->onerow++;
                            break;
                        case 'D5'://setActiveSheetIndex(2)
                            $objPHPExcel->setActiveSheetIndex(2);
                            if($this->twofalg == TRUE)
                            {
                                $objPHPExcel->getActiveSheet()->setTitle('大西豪5楼');
                                $this->setActiveSheet($objPHPExcel, 2, '大西豪5楼');
                            }
                            //调用插入数据函数
                            $this->insertData($this->tworow, 2, 0, $v, $objPHPExcel);
                            $this->tworow++;
                            break;
                        case 'D6'://setActiveSheetIndex(3)
                            $objPHPExcel->setActiveSheetIndex(3);
                            if($this->threefalg == TRUE)
                            {
                                $objPHPExcel->getActiveSheet()->setTitle('大西豪6楼');
                                $this->setActiveSheet($objPHPExcel, 3, '大西豪6楼');
                            }
                            //调用插入数据函数
                            $this->insertData($this->threerow, 3, 0, $v, $objPHPExcel);
                            $this->threerow++;
                            break;
                        case 'C'://setActiveSheetIndex(4)
                            $objPHPExcel->setActiveSheetIndex(4);
                            if($this->fourfalg == TRUE)
                            {
                                $objPHPExcel->getActiveSheet()->setTitle('西街');
                                $this->setActiveSheet($objPHPExcel, 4, '西街');
                            }
                            //调用插入数据函数
                            $this->insertData($this->fourrow, 4, 0, $v, $objPHPExcel);
                            $this->fourrow++;
                            break;
                        case 'X'://setActiveSheetIndex(5)
                            $objPHPExcel->setActiveSheetIndex(5);
                            if($this->fivefalg == TRUE)
                            {
                                $objPHPExcel->getActiveSheet()->setTitle('先潮后人');
                                $this->setActiveSheet($objPHPExcel, 5, '先潮后人');
                            }
                            //调用插入数据函数
                            $this->insertData($this->fiverow, 5, 0, $v, $objPHPExcel);
                            $this->fiverow++;
                            break;
                        case 'N'://setActiveSheetIndex(8)
                            $objPHPExcel->setActiveSheetIndex(8);
                            if($this->eightfalg == TRUE)
                            {
                                $objPHPExcel->getActiveSheet()->setTitle('女人街');
                                $this->setActiveSheet($objPHPExcel, 8, '女人街');
                            }
                            //调用插入数据函数
                            $this->insertData($this->eightrow, 8, 0, $v, $objPHPExcel);
                            $this->eightrow++;
                            break;
                        default://setActiveSheetIndex(6) 其他
                            $objPHPExcel->setActiveSheetIndex(6);
                            if($this->sixfalg == TRUE)
                            {
                                $objPHPExcel->getActiveSheet()->setTitle('其他');
                                $this->setActiveSheet($objPHPExcel, 6, '其他');
                            }
                            //调用插入数据函数
                            $this->insertData($this->sixrow, 6, 0, $v, $objPHPExcel);
                            $this->sixrow++;
                            break;
                    }
                    
                    //仓库出货  setActiveSheetIndex(7)
                    if($v->stockOutflag == 1)
                    {
                        $this->insertflag = 1;//插仓库出货数量
                        $objPHPExcel->setActiveSheetIndex(7);
                        if($this->sevenfalg == TRUE)
                        {
                            $objPHPExcel->getActiveSheet()->setTitle('仓库库存');
                            $this->setActiveSheet($objPHPExcel, 7, '仓库库存');
                        }
                        //调用插入数据函数
                        $this->insertData($this->sevenrow, 7, 1, $v, $objPHPExcel);
                        $this->sevenrow++;
                    }
                }
                else
                {//仅仓库需要出货的   setActiveSheetIndex(7)
                    $this->insertflag = 1;//插仓库数量
                    $objPHPExcel->setActiveSheetIndex(7);
                    if($this->sevenfalg == TRUE)
                    {
                        $objPHPExcel->getActiveSheet()->setTitle('仓库库存');
                        $this->setActiveSheet($objPHPExcel, 7, '仓库库存');
                    }
                    //调用插入数据函数
                    $this->insertData($this->sevenrow, 7, 1, $v, $objPHPExcel);
                    $this->sevenrow++;
                }
            }
            $this->saveExcel($objPHPExcel);
            exit;
        }
        else
        {
            return FALSE;
        }
        
    }
    
    public function saveExcel($objPHPExcel)
    {
        $time = date('Y-m-d');
        $title = $time.'配货单.xls';

        // Set active sheet index to the first sheet, so Excel opens this as the first sheet
        $objPHPExcel->setActiveSheetIndex(0);

        // Redirect output to a client’s web browser (Excel5)
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=$title");
        header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output'); 
    }
    
    public function outPutExcel($objPHPExcel)
    {
        $time = date('Y-m-d');
        $title = $time.'peihuodan.xls';
        $objPHPExcel->setActiveSheetIndex(0);
        $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
        $outputFileName = "d:/fserver/distribution center/$title"; 
        $objWriter->save($outputFileName); 
    }


    public function insertData($row, $index, $insertflag, $v, $objPHPExcel)
    {
        if(!$insertflag)
        {
            $value = $v->marketNum;
        }
        else
        {
            $value = $v->stockOut;
        }
        
        $objPHPExcel->setActiveSheetIndex($index)
                    ->setCellValue('A'.($row+3), $v->ProviderName)
                    ->setCellValue('B'.($row+3), $v->ProviderGoodsNO)
                    ->setCellValue('C'.($row+3), $v->GoodsNO)
                    ->setCellValue('D'.($row+3), $v->SpecName)
                    ->setCellValue('E'.($row+3), $value)
                    ->setCellValue('F'.($row+3), $v->GoodsName)
                    ->setCellValue('G'.($row+3), $v->Price);
    }

    public function updateDataBase()
    {
        $string = read_file('./tmp/tradeGoodsList.php');
        $tradeGoodsList = unserialize($string);
        
        if(!$this->updateTradeFlag())
        {
            echo "系统错误，请联系管理员！";
            exit;
        }
        
        $bFalg = $this->allocate_model->updateData($tradeGoodsList);
        
        echo $bFalg;
        exit;
        
        /*if($bFalg)
        {
            $data = array(
                'msg'=>'更新成功'
            );
        }
        else
        {
            $data = array(
                'msg'=>'更新失败'
            );
        }
        
        $this->load->view('allocate', $data);
        $this->load->view('footer');*/
    }
    
    public function updateTradeFlag()
    {
        $string = read_file('./tmp/tradeList.php');
        $tradeList = unserialize($string);
        
        if(!empty ($tradeList))
        {
            $this->allocate_model->updateFlagInDataBase($tradeList);
        }
        
        return TRUE;
    }


    public function setActiveSheet($objPHPExcel, $index, $title)
    {
        switch ($index)
        {
            case 0:
                $this->zerofalg = FALSE;
                break;
            case 1:
                $this->onefalg = FALSE;
                break;
            case 2:
                $this->twofalg = FALSE;
                break;
            case 3:
                $this->threefalg = FALSE;
                break;
            case 4:
                $this->fourfalg = FALSE;
                break;
            case 5:
                $this->fivefalg = FALSE;
                break;
            case 6:
                $this->sixfalg = FALSE;
                break;
            case 7:
                $this->sevenfalg = FALSE;
                break;
            case 8:
                $this->eightfalg = FALSE;
                break;
            default :
                break;
        }
        
        //设置单元格列宽
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(8);//供应商
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(8);//货号
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(8);//商品编号
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(8);//规格
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(6);//数量
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(36);//商品名称
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(6);//进价

        //设置默认行高
        $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20);
        
        //设置单元格字体，字体大小
        $objPHPExcel->getActiveSheet()->getStyle('A')->getFont()->setName(iconv('gbk', 'utf-8', '宋体'))->setSize(12);
        $objPHPExcel->getActiveSheet()->getStyle('B')->getFont()->setName(iconv('gbk', 'utf-8', '宋体'))->setSize(12);
        $objPHPExcel->getActiveSheet()->getStyle('C')->getFont()->setName(iconv('gbk', 'utf-8', '宋体'))->setSize(12);
        $objPHPExcel->getActiveSheet()->getStyle('D')->getFont()->setName(iconv('gbk', 'utf-8', '宋体'))->setSize(12);
        $objPHPExcel->getActiveSheet()->getStyle('E')->getFont()->setName(iconv('gbk', 'utf-8', '宋体'))->setSize(12);
        $objPHPExcel->getActiveSheet()->getStyle('F')->getFont()->setName(iconv('gbk', 'utf-8', '宋体'))->setSize(12);
        $objPHPExcel->getActiveSheet()->getStyle('G')->getFont()->setName(iconv('gbk', 'utf-8', '宋体'))->setSize(12);
        
        //设置单元格对齐方式														
        $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        $objPHPExcel->getActiveSheet()->getStyle('C')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        $objPHPExcel->getActiveSheet()->getStyle('E')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        $objPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        $objPHPExcel->getActiveSheet()->getStyle('G')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        
        //设置A1背景色
        //$objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
        //$objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->setARGB('FCFCFC');

        //合并第一行
        $objPHPExcel->getActiveSheet()->mergeCells('A1:G1');

        //设置第一行对齐方式
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        
        //设置第一行的内容
        $objPHPExcel->setActiveSheetIndex($index)->setCellValue('A1', $title);
        
        // Add some data
        $objPHPExcel->setActiveSheetIndex($index)
                    ->setCellValue('A2', '供应商')
                    ->setCellValue('B2', '货号')
                    ->setCellValue('C2', '编号')
                    ->setCellValue('D2', '规格')
                    ->setCellValue('E2', '数量')
                    ->setCellValue('F2', '商品编号')
                    ->setCellValue('G2', '进价');
    }
}